/*
 * TltPlotWindow.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   TltPlotWindow.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   7 feb 2013
 */

#include "TltPlotMouseEvent.hpp"

#include "TltPlotWindow.hpp"

namespace tltgen {

  BEGIN_EVENT_TABLE(TltPlotWindow, wxPLplotwindow)
  EVT_MOTION(TltPlotWindow::OnMouseMotion)
  END_EVENT_TABLE()



  TltPlotWindow::TltPlotWindow(wxWindow* parent, wxWindowID id, const wxPoint& pos,
		const wxSize& size, long style, int pl_style) :
  wxPLplotwindow(parent, id, pos, size, style, pl_style)
  {
    
  }

  TltPlotWindow::~TltPlotWindow()
  { }


  void TltPlotWindow::OnMouseMotion(wxMouseEvent& event)
  {
    PLFLT vp_xmin_rel, vp_xmax_rel, vp_ymin_rel, vp_ymax_rel;
    m_stream->gvpd(vp_xmin_rel, vp_xmax_rel, vp_ymin_rel, vp_ymax_rel);
    PLFLT xmin, xmax, ymin, ymax;
    m_stream->gvpw(xmin, xmax, ymin, ymax);

    PLFLT rel_x = ((PLFLT)event.GetX())/GetSize().GetWidth();
    PLFLT rel_y = 1.0 - ((PLFLT)event.GetY())/GetSize().GetHeight();

    PLFLT vp_x = (rel_x - vp_xmin_rel)/(vp_xmax_rel - vp_xmin_rel);
    PLFLT vp_y = (rel_y - vp_ymin_rel)/(vp_ymax_rel - vp_ymin_rel);

    if (0 <= vp_x && vp_x <= 1 &&
	0 <= vp_y && vp_y <= 1) {

      PLFLT x = xmin + vp_x * (xmax-xmin);
      PLFLT y = ymin + vp_y * (ymax-ymin);

      TltPlotMouseEvent tltPlotMouseEvent(GetId(), TLT_MOUSE_MOTION_EVENT, x, y, event);
      tltPlotMouseEvent.SetEventObject(this);
      GetEventHandler()->ProcessEvent(tltPlotMouseEvent);
    }
  }
}



